/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO.Util;
import Pojo.*;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
/**
 *
 * @author An
 */
public class AdminDAO {
    public static List<Admin> LayThongTinAdminTheoUsernamePassword(String username, String password)
    {
        List<Admin> ad = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            String hql = "from Admin ad where ad.tenDangNhap = :username and ad.matKhau = :password";
            Query query = session.createQuery(hql);
            query.setParameter("username",username);
            query.setParameter("password",password);
            ad = query.list();
            
        }finally{
            session.close();
        }       
        return ad;
    }
    
    public static Capdo LayCapdoTheomacapdo(int macapdo)
    {
         Capdo cd = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            cd = (Capdo) session.get(Capdo.class, macapdo);
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
            session.close();
        }
        return cd;
    }
    
    public static List<Capdo> LayDanhSachCapDo()
    {
        List<Capdo> cd = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            String hql = "select cd from Capdo cd";
            Query query = session.createQuery(hql);
            cd = query.list();
        }finally{
            session.close();
        }
        return cd;
    }
    public static boolean  CapNhatCapDoCauHoi(Capdo cd)
    {
        Session session = HibernateUtil.getSessionFactory().openSession();
        boolean kq = true;
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.update(cd);
            transaction.commit();
        } catch (HibernateException ex) {
            //Log the exception
            transaction.rollback();
            System.err.println(ex.getMessages());
            kq = false;
        } finally {
            session.close();
        }
        return kq;
    }
}
